package com.lzm.listendemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.transition.ChangeBounds;
import android.transition.ChangeTransform;
import android.transition.Fade;
import android.transition.TransitionSet;
import android.view.Window;
import android.webkit.WebView;
import android.widget.RelativeLayout;

import com.google.android.material.transition.MaterialFade;
import com.google.android.material.transition.platform.MaterialContainerTransform;
import com.google.android.material.transition.platform.MaterialContainerTransformSharedElementCallback;
import com.google.android.material.transition.platform.MaterialElevationScale;
import com.lzm.listendemo.databinding.ActivityDetailBinding;

public class DetailActivity extends AppCompatActivity {
    private ActivityDetailBinding mBind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        setEnterSharedElementCallback(new MaterialContainerTransformSharedElementCallback());
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_detail);

        RelativeLayout main = findViewById(R.id.main);
        //WebView webView = findViewById(R.id.web);

        String article = getIntent().getStringExtra("article");
        String title = getIntent().getStringExtra("title");
        String source = getIntent().getStringExtra("source");

        mBind.tvTitle.setText(title);
        switch (source){
            case "juejin":
                mBind.ivSource.setImageResource(R.mipmap.ic_juejin);
                break;
            case "weixin":
                mBind.ivSource.setImageResource(R.drawable.ic_wechat);
                break;
            case "csdn":
                mBind.ivSource.setImageResource(R.drawable.ic_csdn);
                break;
            case "wanandroid":
                mBind.ivSource.setImageResource(R.drawable.ic_android);
                break;
            default:
                break;
        }

        ViewCompat.setTransitionName(mBind.tvTitle,"title:" + title );
        ViewCompat.setTransitionName(mBind.ivSource,"source:" + source);

        getWindow().setEnterTransition(new Fade());
        getWindow().setExitTransition(new Fade());

        //webView.getSettings().setJavaScriptEnabled(true);
        //webView.loadUrl(article);
        // 2. 设置transitionName
        main.setTransitionName(getIntent().getStringExtra("MainActivity"));

        TransitionSet transitionSet = new TransitionSet();
        transitionSet.addTransition(new ChangeBounds());
        transitionSet.addTransition(new ChangeTransform());
        transitionSet.addTransition(new Fade());
        transitionSet.addTarget(mBind.tvTitle).setDuration(100L);
        transitionSet.addTarget(mBind.ivSource).setDuration(300L);
        getWindow().setSharedElementEnterTransition(transitionSet);
        getWindow().setSharedElementExitTransition(transitionSet);

        // 3. 设置具体的动画
//        getWindow().setSharedElementEnterTransition(new MaterialContainerTransform().addTarget(mBind.main).setDuration(1500L));
//        getWindow().setSharedElementEnterTransition(new MaterialContainerTransform().addTarget(mBind.main).setDuration(1500L));
//        getWindow().setSharedElementEnterTransition(new MaterialElevationScale(true).addTarget(mBind.tvTitle).setDuration(20000L));
//        getWindow().setSharedElementEnterTransition(new MaterialElevationScale(true).addTarget(mBind.ivSource).setDuration(20000L));

    }
}